﻿package com.wandian.view.mediator
{
	import com.greensock.TweenLite;
	import com.wandian.controller.*;
	import com.wandian.data.GameData;
	import com.wandian.model.*;
	import com.wandian.model.vo.*;
	import com.wandian.util.CLZButton;
	import com.wandian.util.FormatMoney;
	import com.wandian.view.components.ui.AddBtn;
	import com.wandian.view.components.ui.Gift;
	import com.wandian.view.layer.Canvas;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.mediator.*;
	/**   
	* @author long
	* @QQ 108232706
	* @version 1.0   
	*/   
	public class BuyInMediator extends Mediator
	{
		private var totalAssets:Number;
		private var _countDown:Timer;
		private var minChip:Number;
		private var maxChip:Number;
		private var seatId:int;
		private var _global:GameData;
		private var _nSeatId:int=0;
		private var _isAgainBuyIn:int=0;//1为快速买入
		private var _inRoomData:InRoomPlayerDataProxy;
		public static const NAME:String="BuyInMediator";

		public function BuyInMediator(viewComponent:Object=null, data:Object=null)
		{
			this._global=GameData.getInstance();
			this._nSeatId=data.seatId;
			this._isAgainBuyIn=data.againBuyIn;
			trace("快速买入:_nSeatId "+_nSeatId+" againBuyIn "+_isAgainBuyIn);
			super(NAME, viewComponent);
			this._inRoomData=facade.retrieveProxy(InRoomPlayerDataProxy.NAME) as InRoomPlayerDataProxy;
			return;
		}

		override public function onRegister():void
		{
			this.init();
			return;
		}

		private function init():void
		{
			this.mcBuyIn.btn_ok.addEventListener(MouseEvent.CLICK, this.clickBuyInHandler);
			AddBtn.initBtn(this.mcBuyIn as MovieClip, CLZNoticeName.BUY_IN_WINDOS_CLOSE, AddBtn.CLOSE_TWO, "", 2, 2);
			this.mcBuyIn.buyInMin_btn.addEventListener(MouseEvent.CLICK, this.clickBuyInMinHnadler);
			this.mcBuyIn.buyInMax_btn.addEventListener(MouseEvent.CLICK, this.clickBuyInMaxHnadler);
			this.mcBuyIn.sub_btn.addEventListener(MouseEvent.CLICK, this.clickSubHandler);
			this.mcBuyIn.add_btn.addEventListener(MouseEvent.CLICK, this.clickAddHandler);
			this.mcBuyIn.txt_error.addEventListener(TextEvent.LINK, this.linkTxtHandler);
			this.mcBuyIn.title_txt.text=GameData.tip["BUYIN_CHIP_TITLE"];
			this.showBuyIn(this._nSeatId, GameData._objRoom.minStake, GameData._objRoom.maxStake, GameData._objRoom.type, this._isAgainBuyIn);
			return;
		}

		private function linkTxtHandler(event:TextEvent):void
		{
			this.sendNotification(CLZNoticeName.ON_PAY_VIP);
			return;
		}

		private function timerHandler(event:TimerEvent):void
		{
			var _loc_2:*=int(GameData.config["againBuyInTime"]) - this._countDown.currentCount;
			this.mcBuyIn.countDown_txt.htmlText=GameData.replaceStrValue(GameData.tip["BUYIN_COUNTDOWN"], "TIME", _loc_2);
			if (_loc_2 == 0)
			{
				this.sendNotification(CLZNoticeName.BUY_IN_WINDOS_CLOSE);
			}
			return;
		}

		private function completeTimerHandler(event:TimerEvent):void
		{
			this.sendNotification(CLZNoticeName.BUY_IN_WINDOS_CLOSE);
			return;
		}

		private function timerRemove():void
		{
			if (this._countDown != null)
			{
				this._countDown.removeEventListener(TimerEvent.TIMER, this.timerHandler);
				this._countDown.removeEventListener(TimerEvent.TIMER_COMPLETE, this.completeTimerHandler);
				this._countDown=null;
			}
			return;
		}

		public function showBuyIn(_seatId:int, _minChip:Number, _maxChip:Number, roomtype:String, _isAgainBuyIn:int=0):void
		{
			this.mcBuyIn.countDown_txt.text="";
			if (_isAgainBuyIn == 1)
			{
				this.mcBuyIn.countDown_txt.htmlText=GameData.replaceStrValue(GameData.tip["BUYIN_COUNTDOWN"], "TIME", GameData.config["againBuyInTime"]);
				this._countDown=new Timer(1000, int(GameData.config["againBuyInTime"]));
				this._countDown.addEventListener(TimerEvent.TIMER, this.timerHandler);
				this._countDown.addEventListener(TimerEvent.TIMER_COMPLETE, this.completeTimerHandler);
				this._countDown.start();
			}
			this.mcBuyIn.txt_error.text="";
			this.mcBuyIn.txt_buyIn.text="";
			this.seatId=_seatId;
			this.minChip=_minChip;
			this.maxChip=_maxChip;
			CLZButton.setButton(this.mcBuyIn.btn_ok, GameData.tip["OK_BUYIN_NAME"]);
			this.mcBuyIn.buyInMinName_txt.text=GameData.tip["BUYIN_MIN_NAME"];
			this.mcBuyIn.buyInMaxName_txt.text=GameData.tip["BUYIN_MAX_NAME"];
//			if (this._global.judgeMoneyType(param4))
//			{
				this.totalAssets=this._global.userInfo.userVo.money;
				this.mcBuyIn.totalAssetsName_txt.text=GameData.tip["BUYIN_SUMGOLD_NAME"];
//			}
//			else
//			{
//				this.totalAssets=this._global.userInfo.userVo.money;
//				this.mcBuyIn.totalAssetsName_txt.text=GameData.tip["BUYIN_SUMMONEY_NAME"];
//			}
			this.mcBuyIn.txt_totalAssets.text=FormatMoney.setFormatMoney(this.totalAssets);
			this.mcBuyIn.totalAssets=FormatMoney.setFormatMoney(this.totalAssets);
			this.mcBuyIn.txt_buyInMin.text=FormatMoney.setFormatMoney(_minChip);
			this.mcBuyIn.txt_buyInMax.text=FormatMoney.setFormatMoney(_maxChip);
			if (this.totalAssets < this.minChip)
			{
				this.mcBuyIn.txt_buyIn.text="0";
			}
			else
			{
				this.mcBuyIn.txt_buyIn.text=this.minChip.toString();
			}
			if (this._isAgainBuyIn)//1.快速买入
			{
				this.clickBuyInHandler(null);
			}
			return;
		}

		private function clickSubHandler(event:MouseEvent):void
		{
			var buyinMoney:*=int(this.mcBuyIn.txt_buyIn.text);
			if (buyinMoney - this.minChip < this.minChip)
			{
				this.mcBuyIn.txt_buyIn.text=this.minChip.toString();
				return;
			}
			this.mcBuyIn.txt_buyIn.text=(buyinMoney - this.minChip).toString();
			return;
		}

		private function clickAddHandler(event:MouseEvent):void
		{
			var _loc_2:*=int(this.mcBuyIn.txt_buyIn.text);
			if (_loc_2 + this.minChip > this.maxChip)
			{
				this.mcBuyIn.txt_buyIn.text=this.maxChip.toString();
				return;
			}
			this.mcBuyIn.txt_buyIn.text=(_loc_2 + this.minChip).toString();
			return;
		}

		public function clickBuyInHandler(event:MouseEvent):void
		{
			var buyinMoney:Number=Number(this.mcBuyIn.txt_buyIn.text);
			if (buyinMoney <= this.totalAssets)
			{
				if (this.minChip > this.totalAssets)
				{
					if (this._global.judgeMoneyType(GameData._objRoom.type))
					{
						this.mcBuyIn.txt_error.htmlText=GameData.tip["GAME_GOLD_NOT_ENOUGH"] + "<a href=\"event:pay\">" + GameData.replaceStrTipHtml(GameData.tip["GET_MORE_GLOD"]) + "</a>";
					}
					else
					{
						this.mcBuyIn.txt_error.htmlText=GameData.tip["GAME_MONEY_NOT_ENOUGH"];
					}
					return;
				}
			}		
			if (buyinMoney < this.minChip)
			{
				this.mcBuyIn.txt_error.text=GameData.tip["BUY_IN_MONEY_TOO_FEW"];//买入数不能少于最小携带数
				return;
			}
			if (buyinMoney > this.maxChip)
			{
				this.mcBuyIn.txt_error.text=GameData.tip["BUY_IN_MONEY_TOO_MONEY"];//买入数不能大于最大携带数
				return;
			}
			this.buyInSit(buyinMoney);
			return;
		}

		private function clickBuyInMinHnadler(event:MouseEvent):void
		{
			if (this.totalAssets < this.minChip)
			{
//				if (this._global.judgeMoneyType(GameData._objRoom.type))
//				{
					this.mcBuyIn.txt_error.htmlText=GameData.tip["GAME_GOLD_NOT_ENOUGH"] + "<a href=\"event:pay\">" + GameData.replaceStrTipHtml(GameData.tip["GET_MORE_GLOD"]) + "</a>";
//				}
//				else
//				{
//					this.mcBuyIn.txt_error.htmlText=GameData.tip["GAME_MONEY_NOT_ENOUGH"];
//				}
				return;
			}
			this.buyInSit(this.minChip);
			return;
		}

		private function clickBuyInMaxHnadler(event:MouseEvent):void
		{
			if (this.totalAssets >= this.minChip)
			{
			}
			if (this.totalAssets < this.maxChip)
			{
				this.buyInSit(this.totalAssets);
				return;
			}
			if (this.totalAssets >= this.maxChip)
			{
				this.buyInSit(this.maxChip);
			}
			return;
		}

		private function buyInSit(money:Number):void
		{
			if (this._isAgainBuyIn)
			{
				this._isAgainBuyIn=0;
				this.sendNotification(CLZNoticeName.AGAIN_SIT_BUYIN, {seatId: this.seatId, money: money});
			}
			else
			{
				this.sendNotification(CLZNoticeName.SITDOWN, {seatId: this.seatId, money: money});
			}
			this.sendNotification(CLZNoticeName.BUY_IN_WINDOS_CLOSE);
			return;
		}

		public function clickCloseHandler():void
		{
			if (this._countDown != null)
			{
				this._countDown.reset();
			}
			this._isAgainBuyIn=0;
			this.timerRemove();
			if (Texas.I._mcWindosView.contains(this.mcBuyIn))
			{
				Texas.I._mcWindosView.removeChildTo(this.mcBuyIn, Canvas.HIDE_MOVE);
				viewComponent=null;
			}
			return;
		}

		public function get nSeatId():int
		{
			return this.seatId;
		}

		override public function listNotificationInterests():Array
		{
			return [CLZNoticeName.BUY_IN_WINDOS_CLOSE];
		}

		override public function handleNotification(param1:INotification):void
		{
			switch (param1.getName())
			{
				case CLZNoticeName.BUY_IN_WINDOS_CLOSE:
				{
					if (this._isAgainBuyIn)
					{
						this.sendNotification(CLZNoticeName.STAND_UP);
					}
					this.clickCloseHandler();
					facade.removeMediator(BuyInMediator.NAME);
					break;
				}
				default:
				{
					break;
				}
			}
			return;
		}

		private function get mcBuyIn():MovieClip
		{
			return viewComponent as MovieClip;
		}

	}
}
